--
-- Author: mm
-- Date: 2018-03-24 15:51:27
--
-- NewguildPreController
-- 查找、创建工会相关controller

local NewguildSearchView = import("..views.NewguildSearchView")
local NewguildCreateView = import("..views.NewguildCreateView")
local NewCreateGuildSureUI = import("..views.NewCreateGuildSureUI")

local fileName = string.getModeName(...)
local ClassRef = class(fileName, app.mvc.ControllerBase)

function ClassRef:ctor(data)
    self.super.ctor(self, data)
end

function ClassRef:init()
    self._model = app:getInst("NewguildModel")
    self._flagIdx = 1
end

function ClassRef:removeListeners()
    self.super.removeListeners(self)
end

function ClassRef:onEvent(eventName, data)
end

function ClassRef:listEvent()
    return {}
end


function ClassRef:openView(data)
    if cc.isDead(self._view) then
        self._model:reqGuildList( 0 ) -- 新展开UI从第一页取

        self._view = NewguildSearchView.new(app.mvc.VIEWTYPE_BLUR)
        self._view:onCloseView(handler(self, self.closeView))
        self._view:onClearView(handler(self, self.clearView))
        self._view:onOperate(handler(self, self.onOperate))
        self:addChild(self._view, data)
    end

    self._view:show()
    self._view:checkGuide()
end


function ClassRef:clearView()
    if self._view then
        self._view = nil
    end
    self._flagIdx = 1
    self._model:clearPreCreateData()
end

-------------------------------------------------------
function ClassRef:openCreateView(data)
    --打开createView的时候要隐藏
    if self._view then
        self._view:hide()
    end
    if nil == self._createView then
        local baseColorList = self._model:getBaseIconColorList()
        local additionalColorList = self._model:getAdditionalIconColorList()

        self._createView = NewguildCreateView.new( app.mvc.VIEWTYPE_BLUR, { baseColorList, additionalColorList } )
        self._createView:onCloseView(handler(self, self.closeCreateView))
        self._createView:onClearView(handler(self, self.clearCreateView))
        self._createView:onOperate(handler(self, self.onOperate))
        self:addChild(self._createView, data)
    end
    self._createView:show()
end
function ClassRef:closeCreateView(data)
    if self._createView then
        self:removeChild(self._createView, data)
        self:clearCreateView()
    end
    if self._view then
        self._view:show()
    end
end
function ClassRef:clearCreateView()
    if self._createView then
        self._createView = nil
    end
end

-------------------------------------------------------
function ClassRef:openCreateSureView(data)
    if nil == self._createSureView then
        self._createSureView = NewCreateGuildSureUI.new( app.mvc.VIEWTYPE_BLUR, data )
        self._createSureView:onCloseView(handler(self, self.closeCreateSureView))
        self._createSureView:onClearView(handler(self, self.clearCreateSureView))
        self._createSureView:onOperate(handler(self, self.onOperate))
        self:addChild(self._createSureView, data)
    end
    self._createSureView:show()
end
function ClassRef:closeCreateSureView(data)
    if self._createSureView then
        self:removeChild(self._createSureView, data)
        self:clearCreateSureView()
    end
end
function ClassRef:clearCreateSureView()
    if self._createSureView then
        self._createSureView = nil
    end
end

-------------------------------------------------------
-- 刷新公会列表
function ClassRef:refreshGuildList( guildInfoList )
    if not self:isViewAlive() then return end
    local deltaLen = self._flagIdx - self._model:getCurGuildListStartIdx() + 1
    self._view:refreshGuildData(guildInfoList, deltaLen)

end

function ClassRef:refreshGuildListData(guildInfoList )
    if not self:isViewAlive() then return end
    self._view:refreshGuildDataOnly(guildInfoList)
end

-------------------------------------------------------
function ClassRef:onOperate(op, data)
    --print("NewguildCreateController:onOperate op = " .. op)
    if op == "selected_Guild" then -- 查看具体公会信息
        if not self:isViewAlive() then return end
        self._view:refreshGuildInfo( data )
    elseif op == "applyJoin_Guild" then -- 申请加入公会
        self._model:reqApplyGuild( data:getGuildId() )
    elseif op == "search_guild" then -- 搜索具体公会
        if app:isNeedCheckInput() then
                app:checkInput(data, function(response,errorMsg,code)
                    if code == 200 then
                        if response and response.result then
                            self._model:reqQueryGuild(data)
                        else
                            display.pushToast(L("lua_code_text_11"))
                        end
                    else
                        display.pushToast(L("network_error"))
                    end
                end)
            else
                self._model:reqQueryGuild(data)
            end
    elseif op == "closeSearch_guild" then -- 取消搜索具体公会
        self._model:reqGuildList(self._flagIdx)
    elseif op == "quickAddGuild" then -- 一键申请
        self._model:reqQuickApplyGuild()
    elseif op == "preCreata_guild" then -- 创建公会预览View
        self:openCreateView()
    elseif op == "create_guild" then -- 确认创建公会
        local createGuildInfo = self._createView:getPreCreateInfo()
        if self:_cheackGuildParam( createGuildInfo ) then -- 参数合法？
            if app:isNeedCheckInput() then -- 存在检测地址
                app:checkInput( createGuildInfo.name, function(response,errorMsg,code)
                    if code == 200 then
                        if response and response.result then
                            self:openCreateSureView( createGuildInfo )
                        else
                            display.pushToast(L("lua_code_text_163"))
                        end
                    else
                        display.pushToast(L("network_error"))
                    end
                end)
            else
                self:openCreateSureView( createGuildInfo )
            end
        else
            display.pushToast( L("guild_illegal") )
            return
        end
    elseif op == "create_guild_sure" then -- 确认创建
        self:closeCreateSureView( data )
        self._model:reqCreateGuild( data )
    elseif op == "reqGuildListEnd" then
        self._flagIdx = self._model:getCurGuildListEndIdx()
        self._model:reqGuildList(self._flagIdx)
    elseif op == "reqGuildListStart" then
        self._flagIdx = self._model:getCurGuildListStartIdx()
        self._model:reqGuildList(self._flagIdx)
    end
end

-- 检查 创建公会 参数合法性
function ClassRef:_cheackGuildParam( createGuildInfo )
    local baseIconConf = GD:queryGuildCreateById( "flagIconBig" )
    local additionalIconConf = GD:queryGuildCreateById( "flagIconLittle" )
    local baseColorList = self._model:getBaseIconColorList()
    local additionalColorList = self._model:getAdditionalIconColorList()

    local valid = true -- 默认合法
    if not createGuildInfo.name or ""  == createGuildInfo.name then -- 名字为空
        print( " ---------->> _cheackGuildParam 名字非法")
        valid = false
    end
    if not createGuildInfo.flagBaseIcon or
        "" == createGuildInfo.flagBaseIcon or
        not string.find( baseIconConf.strPar, createGuildInfo.flagBaseIcon ) then -- 基础图案Id非法
        print( " ---------->> _cheackGuildParam 基础图案Id非法")
        valid = false
    end
    if not createGuildInfo.flagAdditionalIcon or
        "" == createGuildInfo.flagAdditionalIcon or
        not string.find( additionalIconConf.strPar, createGuildInfo.flagAdditionalIcon ) then -- 附加图案Id非法
        print( " ---------->> _cheackGuildParam 附加图案Id非法")
        valid = false
    end
    if not createGuildInfo.flagBaseIconColor
        or not ( 0 < createGuildInfo.flagBaseIconColor and createGuildInfo.flagBaseIconColor <= #baseColorList ) then -- 基础图案颜色Id非法
        print( " ---------->> _cheackGuildParam 基础图案颜色Id非法")
        valid = false
    end
    if not createGuildInfo.flagAdditionalIconColor
        or not ( 0 < createGuildInfo.flagAdditionalIconColor and createGuildInfo.flagAdditionalIconColor <= #additionalColorList ) then -- 附加图案颜色Id非法
        print( " ---------->> _cheackGuildParam 附加图案颜色Id非法")
        valid = false
    end
    return valid
end

return ClassRef
